﻿Imports System.ComponentModel

Class MainWindow

    Dim PrinterCtrls As New List(Of PrinterControl)
    Dim PrinterThread As New PrinterThread

    Private Sub MainWindow_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
        GetServerConfig()
        GetPrinterConfig()
        ServerReady()
    End Sub

    Private Sub MainWindow_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        PrinterThread.Thead_Close()
    End Sub

    Private Sub ServerReady()
        ServerStart.Visibility = Visibility.Visible
        ServerPause.Visibility = Visibility.Hidden
        PrinterEdit.Visibility = Visibility.Visible
        PrinterSave.Visibility = Visibility.Hidden
        PrinterThread.SetLabel(StateText)
    End Sub

    Private Sub GetServerConfig()
        WM_SERVER.Text = My.Settings.WM_SERVER
        WM_APPID.Text = My.Settings.WM_APPID
        WM_TOKEN.Password = My.Settings.WM_TOKEN
        WM_TIMER.Text = My.Settings.WM_TIMER
        VersionText.Content = My.Settings.WM_VERSION
    End Sub

    Private Sub SetServerConfig()
        My.Settings.WM_SERVER = WM_SERVER.Text
        My.Settings.WM_APPID = WM_APPID.Text
        My.Settings.WM_TOKEN = WM_TOKEN.Password
        My.Settings.WM_TIMER = WM_TIMER.Text
        My.Settings.Save()
    End Sub

    Private Sub GetPrinterConfig()
        PrinterCtrls.Add(Printer1)
        PrinterCtrls.Add(Printer2)
        PrinterCtrls.Add(Printer3)
        PrinterCtrls.Add(Printer4)

        For Each PrinterCtrl In PrinterCtrls

            PrinterCtrl.PrinterList.Items.Add("")
            For Each printName In System.Drawing.Printing.PrinterSettings.InstalledPrinters
                PrinterCtrl.PrinterList.Items.Add(printName)
            Next
            PrinterCtrl.PrinterType.Items.Add("全部")
            PrinterCtrl.PrinterType.Items.Add("模版1")
            PrinterCtrl.PrinterType.Items.Add("模版2")
            PrinterCtrl.PrinterType.Items.Add("模版3")
            PrinterCtrl.PrinterType.Items.Add("模版4")
            PrinterCtrl.IsEnabled = False
        Next

        Printer1.LoadPrinterConfig(My.Settings.Printer_1)
        Printer2.LoadPrinterConfig(My.Settings.Printer_2)
        Printer3.LoadPrinterConfig(My.Settings.Printer_3)
        Printer4.LoadPrinterConfig(My.Settings.Printer_4)
    End Sub

    Private Sub SetPrinterConfig()
        For Each PrinterCtrl In PrinterCtrls
            PrinterCtrl.IsEnabled = False
        Next
        My.Settings.Printer_1 = Printer1.GetPrinterConfig(True)
        My.Settings.Printer_2 = Printer2.GetPrinterConfig(True)
        My.Settings.Printer_3 = Printer3.GetPrinterConfig(True)
        My.Settings.Printer_4 = Printer4.GetPrinterConfig(True)

        My.Settings.Save()
    End Sub

    Private Sub ConfigEdit_Click(sender As Object, e As RoutedEventArgs) Handles ConfigEdit.Click
        WM_SERVER.IsEnabled = True
        WM_APPID.IsEnabled = True
        WM_TOKEN.IsEnabled = True
        WM_TIMER.IsEnabled = True
        ConfigEdit.Visibility = Visibility.Hidden
        ConfigSave.Visibility = Visibility.Visible
    End Sub

    Private Sub ConfigSave_Click(sender As Object, e As RoutedEventArgs) Handles ConfigSave.Click
        SetServerConfig()
        WM_SERVER.IsEnabled = False
        WM_APPID.IsEnabled = False
        WM_TOKEN.IsEnabled = False
        WM_TIMER.IsEnabled = False
        ConfigEdit.Visibility = Visibility.Visible
        ConfigSave.Visibility = Visibility.Hidden
    End Sub

    Private Sub ConfigTest_Click(sender As Object, e As RoutedEventArgs) Handles ConfigTest.Click
        Dim HttpServer = New HttpServer
        HttpServer.StateText = StateText
        HttpServer.TestConnect()
    End Sub

    Private Sub PrinterEdit_Click(sender As Object, e As RoutedEventArgs) Handles PrinterEdit.Click
        PrinterEdit.Visibility = Visibility.Hidden
        PrinterSave.Visibility = Visibility.Visible
        ServerStart.Visibility = Visibility.Hidden
        For Each PrinterCtrl In PrinterCtrls
            PrinterCtrl.IsEnabled = True
        Next
    End Sub

    Private Sub PrinterSave_Click(sender As Object, e As RoutedEventArgs) Handles PrinterSave.Click
        PrinterEdit.Visibility = Visibility.Visible
        PrinterSave.Visibility = Visibility.Hidden
        ServerStart.Visibility = Visibility.Visible
        SetPrinterConfig()
    End Sub

    Private Sub ServerStart_Click(sender As Object, e As RoutedEventArgs) Handles ServerStart.Click
        PrinterEdit.Visibility = Visibility.Hidden
        ServerStart.Visibility = Visibility.Hidden
        ServerPause.Visibility = Visibility.Visible
        StateText.Content = "就绪"
        PrinterThread.Thead_Start()
    End Sub

    Private Sub ServerPause_Click(sender As Object, e As RoutedEventArgs) Handles ServerPause.Click
        PrinterEdit.Visibility = Visibility.Visible
        ServerStart.Visibility = Visibility.Visible
        ServerPause.Visibility = Visibility.Hidden
        StateText.Content = "暂停"
        PrinterThread.Thead_Pause()
    End Sub


End Class
